#include "iooperationimpl.h"

CIoOperationImpl::CIoOperationImpl()
{
	m_pBufLen = 4 * 1024;
	m_pBuf = NULL;
	m_pBuf = ALLOC_NEW BYTE[m_pBufLen + 1]();
	m_uStartPos = 0;
	m_uExpectBytes = 0;
}
CIoOperationImpl::~CIoOperationImpl()
{
	m_pEvent.dispose();
	logi("CIoOperationImpl::~CIoOperationImpl");
}
std_method_impl CIoOperationImpl::BindIBase(IBase* pBase)
{
	rc_assert(pBase != INULL, E_FAIL)
	m_pBase.dispose();
	return pBase->QueryInterface(IID_IBase,(void**)&m_pBase);
}
std_method_impl CIoOperationImpl::UnBindIBase()
{
	m_pBase.dispose();
	return S_OK;
}
std_method_impl CIoOperationImpl::QueryIBaseIID(REFIID riid, void **ppv)
{
	rc_assert(m_pBase.m_p != INULL, E_FAIL)
	return m_pBase->QueryInterface(riid, (void**)ppv);
}
std_method_impl CIoOperationImpl::BindEvent(IBase *pEvents)
{
	HRESULT hr = S_OK;
	hr = pEvents->QueryInterface(IID_IOperationEvent, (void**)&m_pEvent);
	rc_assert(hr == S_OK, E_FAIL)
	return hr;
}
std_method_impl CIoOperationImpl::UnBindEvent()
{
	m_pEvent.dispose();
	return S_OK;
}
std_method_impl CIoOperationImpl::SetId(ULONG uId)
{
	m_id = uId;
	return S_OK;
}
std_method_type_impl(ULONG) CIoOperationImpl::GetId()
{
	return m_id;
}
std_method_impl CIoOperationImpl::SetResult(ERROR_CODE Error, BUFFER_SIZE uTransferedBytes)
{
	m_Errorcode			= Error;
	m_pTransferedBytes	= uTransferedBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetResult(ERROR_CODE *Error, BUFFER_SIZE *pTransferedBytes)
{
	*Error				= m_Errorcode;
	*pTransferedBytes	= m_pTransferedBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::SetIoParam(ULONG uStartPos, ULONG uExpectBytes)
{
	if (uExpectBytes - uStartPos > m_pBufLen) {
		return E_FAIL;
	}
	m_uStartPos = uStartPos;
	m_uExpectBytes = uExpectBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetIoParam(ULONG* uStartPos, ULONG* pExpectBytes)
{
	*uStartPos = m_uStartPos;
	*pExpectBytes = m_uExpectBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::SetExpect(ULONG uExpectBytes)
{
	m_uExpectBytes = uExpectBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetExpect(ULONG *pExpectBytes)
{
	*pExpectBytes = m_uExpectBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::SetPos(ULONG uPos)
{
	m_uStartPos = uPos;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetPos(ULONG *pPos)
{
	*pPos = m_uStartPos;
	return S_OK;
}
std_method_impl CIoOperationImpl::SetTransfered(BUFFER_SIZE uTransferedBytes)
{
	m_pTransferedBytes = uTransferedBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetTransfered(BUFFER_SIZE *pTransferedBytes)
{
	*pTransferedBytes = m_pTransferedBytes;
	return S_OK;
}
std_method_impl CIoOperationImpl::Attach(BUFFER_PTR Ptr, BUFFER_SIZE Pos, BUFFER_SIZE Size)
{
	rc_assert(Ptr != NULL, E_FAIL)
	rc_assert(m_pBuf.m_p != NULL, E_FAIL)
	s_memcpy(m_pBuf.m_p + Pos, Ptr, Size);
	return 	SetIoParam(Pos, Size);
}
std_method_impl CIoOperationImpl::Alloc(BUFFER_SIZE Size)
{
	m_pBuf.dispose();
	m_pBufLen = Size;
	m_pBuf = ALLOC_NEW unsigned char[m_pBufLen + 1]();
	rc_assert(m_pBuf.m_p != NULL,E_FAIL)
	return S_OK;
}
std_method_impl CIoOperationImpl::GetLen(BUFFER_SIZE *Len)
{
	*Len = m_pBufLen;
	return S_OK;
}
std_method_impl CIoOperationImpl::GetPtr(BUFFER_PTR* Ptr)
{
	rc_assert(m_pBuf.m_p != NULL, E_FAIL)
	*Ptr = m_pBuf.m_p;
	return S_OK;
}
std_method_impl CIoOperationImpl::ReleasePtr()
{
	rc_assert(m_pBuf.m_p != NULL, E_FAIL)
	m_pBuf.dispose();
	m_pBufLen = 0;
	return S_OK;
}
std_method_impl CIoOperationImpl::ClearPtr()
{
	rc_assert(m_pBuf.m_p != NULL, E_FAIL)
	s_memset(m_pBuf, 0, m_pBufLen);
	return S_OK;
}
std_method_impl CIoOperationImpl::Post(ULONG event, ULONG error_code)
{
	rc_assert(m_pEvent.m_p != INULL,E_FAIL)
	return m_pEvent->OnComplete(this, event, error_code);
}
